var subordinate={

	init:function(){
		var that = this;
		$(".jaddSub").on('click',function(){
			$("#SubordinateWrap").hide();
			$("#SubordinateAddWrap").show();
			that.doSubmit();
		})

		$("#cancelRed").on("click", function(){
			window.location.reload();
		})

		$.ajax({
            url: COMMONURL + "/api/account/getlist?token=" + $.cookie('token'),
            type: "post",
            data: {

            },
            dataType: "json"
        }).done(function(redata) {
        		if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;}
            if (redata.recode == '0000') {
                var compiled = _.template($("#subordinateTmp").html());
                var odata = redata.mod.Items;
               
                $("#SubordinateCont").html(compiled(odata));


                $(".jDoneBtn").on("click", function(){
                	var id = $(this).attr("data-id");

                	      $.ajax({
					            url: COMMONURL + "/api/account/updatestate?token=" + $.cookie('token'),
					            type: "get",
					            data: {
					            	id:id,
					            	state:1
					            },
					            dataType: "json"
					        }).done(function(redata) {
                               if(redata.recode == '0000'){
                               	  alert("操作成功！");
                               	  window.location.reload();
                               }
					        })
                })

                $(".jStopBtn").on("click", function(){
                	var id = $(this).attr("data-id");

                	      $.ajax({
					            url: COMMONURL + "/api/account/updatestate?token=" + $.cookie('token'),
					            type: "get",
					            data: {
					            	id:id,
					            	state:10
					            },
					            dataType: "json"
					        }).done(function(redata) {
                               if(redata.recode == '0000'){
                               	  alert("操作成功！");
                               	  window.location.reload();
                               }
					        })
                })

                $(".jOutBtn").on("click", function(){
                	var id = $(this).attr("data-id");

                	      $.ajax({
					            url: COMMONURL + "/api/account/updatestate?token=" + $.cookie('token'),
					            type: "get",
					            data: {
					            	id:id,
					            	state:-1
					            },
					            dataType: "json"
					        }).done(function(redata) {
                               if(redata.recode == '0000'){
                               	  alert("操作成功！");
                               	  window.location.reload();
                               }
					        })
                })


              }
          });
	},

	doSubmit:function(){
		 $("#SubordinateAddForm").validate({
            errorClass: 'error',
            rules: {
                subusername: "required",
                subname:"required",
                subpassword:"required",
                comsubpassword:{
                	required:true,
                	equalTo:"#subpassword"
                },
                subphone:"required"
                
            },
            messages: {
                subusername: "",
               subname:"",
               subpassword:"",
               comsubpassword:{
               	required:"",
               	equalTo:"两次输入密码不一致"
               },
               subphone:''
            },
            submitHandler: function(form) {
            	var data={
            		account:$("#subusername").val(),
            		nick_name:$("#subname").val(),
            		password:$("#subpassword").val(),
            		phone:$("#subphone").val(),
            		identity:$("#subType option:selected").val()
            	}
            	$.ajax({
                    url: COMMONURL + "api/account/add?token=" + $.cookie('token'),
                    type: "post",
                    dataType: "json",
                    data: data,
                }).done(function(data) {
                	if(data.recode == '0000'){
                		  alert('操作成功');
                       window.location.reload();
                        }else{
                        	 alert(data.resmessage);
                        }
                });
            }
        });


	}

  
}

subordinate.init();